export default function ajaxInterceptor() {
  'ngInject';

  const SERVER_CONTEXT = '/sgms';
  return {
    /* eslint no-param-reassign:0 */
    request(config) {
      // if (process.env.NODE_ENV === 'development' && !process.mock) {
      //   if (/api\/v1/.test(config.url)) {
      //     config.url = SERVER_CONTEXT + config.url;
      //   }
      // }
      if (/api/.test(config.url) || /api\/v1/.test(config.url)) {
        config.url = SERVER_CONTEXT + config.url;
      }
      return config;
    },
    response(response) {
      // 403 code means intercepted by server with not logged reason
      if (response.data && response.data.code === '403') {
        window.location.href = '/#!/login';
      } else if (response.data
          && (response.data.success === true || response.data.success === false)) {
        return response.data;
      }
      return response;
    },
  };
}
